package com.hbzhit.common.extension;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbzhit.common.entity.BaseEntity;
import com.hbzhit.common.utils.PageUtils;
import com.hbzhit.common.utils.PoiUtils;
import com.hbzhit.common.utils.Query;
import com.hbzhit.common.validator.ValidatorUtils;
import com.hbzhit.common.validator.group.AddGroup;
import com.hbzhit.common.validator.group.UpdateGroup;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.core.io.InputStreamSource;
import org.springframework.http.ResponseEntity;

import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Service基类
 * @author DING
 * @date 2021/4/6 15:54
 * @version 1.0
 **/
public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements IBaseService<T> {
    protected QueryWrapper<T> queryWrapperHandle(Map<String, Object> params) {
        return new Query<T>().initQueryWrapper(params);
    }
    
    @Override
    public PageUtils<T> page(Map<String, Object> params) {
        return new PageUtils<>(super.page(new Query<T>().getPage(params), this.queryWrapperHandle(params)));
    }

    @Override
    public List<T> list(Map<String, Object> params) {
        return super.list(this.queryWrapperHandle(params));
    }

    @Override
    public ResponseEntity<byte[]> exportExcel(Map<String, Object> params) throws IOException {
        String name = this.getEntityClass().getSimpleName();

        List<T> list = this.list(params);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(name, "Sheet1"), this.getEntityClass(), list);

        return PoiUtils.export(workbook, name + DateUtil.formatDateTime(new Date()) + ".xls");
    }

    @Override
    public ExcelImportResult<T> importExcel(InputStreamSource source) throws Exception {
        //执行导入
        ExcelImportResult<T> result = PoiUtils.importExcel(source.getInputStream(), this.getEntityClass());

        // 批量写入数据库
        this.saveBatch(result.getList());
        
        return result;
    }

    @Override
    public boolean save(T entity) {
        ValidatorUtils.validateEntity(entity, AddGroup.class);
        return super.save(entity);
    }

    @Override
    public boolean updateById(T entity) {
        ValidatorUtils.validateEntity(entity, UpdateGroup.class);
        return super.updateById(entity);
    }

    @Override
    public boolean updateBatchById(Collection<T> entityList) {
        ValidatorUtils.validateEntityList(entityList, UpdateGroup.class);
        return super.updateBatchById(entityList);
    }
}
